<!-- prompt restriction_form partial, expects a local variable called "challenge_form" and @challenge, as well as boolean "is_offer"  -->
<% if is_offer %>
  <% @challenge.build_offer_restriction unless @challenge.offer_restriction %>
  <% restriction = @challenge.offer_restriction %>
<% else %>
  <% @challenge.build_request_restriction unless @challenge.request_restriction %>
  <% restriction = @challenge.request_restriction %>
<% end %>

<%= challenge_form.fields_for(is_offer ? :offer_restriction : :request_restriction) do |prompt_restriction_form| %>

  <fieldset class="preface profile">

    <% if is_offer %>
      <!-- skip the notes -->
    	<legend>Offer Settings</legend>
    	<h3>Offer Settings</h3>
    	<p class="note">
    	  These settings determine what each separate offer can contain. Note these can be entirely different from the request settings!
    	</p>
    <% else %>
    	<legend>Request Settings</legend>
    	<h3>Request Settings</h3>
    	<p class="note">
    	  These settings determine what each separate request can contain, and in particular how many different tags of each kind.
    	  If you plan to use automated matching, keep in mind it will be slower the more tags you allow people to request.
    	</p>
    	<p class="note">
    	  If you check "Allow Any?" then users will be able to select "Any" for that field, which means they will match no matter 
    	  what the person offering has put into that field. Note: this option is generally safer for offers than requests!
    	</p>
    	<p class="note">
    	  If you check "Must Be Unique?" then users will not be allowed to repeat the same value across prompts. That is, if 
    	  you allow three prompts and specify that fandom must be unique, the user will have to specify completely different fandoms
    	  for all three prompts -- no overlap allowed. Please explain this in your signup instructions!
    	</p> 
    <% end %>
    <dl>
      <%= prompt_restriction_settings(prompt_restriction_form, (is_offer ? false : true)) %>
    </dl>
  </fieldset>

  <% if show_tag_options %>
    <fieldset class="preface profile">
      <legend>Tag Options</legend>
      <h3>Tag Options <%= link_to_help("prompt-restriction-tag-set") %></h3>
      <% restriction.build_tag_set unless restriction.tag_set %>
      <%= prompt_restriction_form.fields_for :tag_set_attributes do |tag_set_form| %>
        <% TagSet::TAG_TYPES.each do |tag_type| %>
          <fieldset class="tag_set">
            <legend><%= ts("%{tag_type} Settings", :tag_type => tag_type.classify) %></legend>
            <h4><%= tag_set_form.label "#{tag_type}_tagnames".to_sym, "#{tag_type.classify}: " %></h4>
            <p>
              <!-- 
                monstrous kludge required by ugly bug in nested_attributes_for, see
                https://rails.lighthouseapp.com/projects/8994/tickets/2646-validations-not-called-when-model-updating-using-nested-attributes
              -->
              <%= tag_set_form.hidden_field :updated_at, :value => Time.now %>
              <% tagnames = restriction.tag_set ? restriction.tag_set.send("#{tag_type}_tagnames") : "" %>
              <%= tag_set_form.text_area "#{tag_type}_tagnames".to_sym, :value => tagnames,
                  :rows => (tagnames.size/3 > 10 ? 10 : (tagnames.size/3 < 2 ? 2 : tagnames.size/3)) %>
              <% tag_fieldname = "#{@challenge.class.name.tableize.singularize}_offer_restriction_attributes_tag_set_attributes_#{tag_type}_tagnames" %>
              <%= autocomplete_text_field(tag_fieldname, :methodname => "canonical_#{tag_type}_finder") %>
            </p>

            <% if TagSet::TAG_TYPES_INITIALIZABLE.include?(tag_type) %>
              <h5><%= ts("Initialize tag list: ") %> <%= link_to_help "challenge-initialize-tags" %></h5>
              <ul>
                <li>
                  <%= prompt_restriction_form.label "#{tag_type}_init_less_than_average", ts("Below the average: ") %>
                  <%= prompt_restriction_form.check_box "#{tag_type}_init_less_than_average" %>
                </li>
                <li>
                  <%= prompt_restriction_form.label "#{tag_type}_init_greater_than_average", ts("Above the average: ") %>
                  <%= prompt_restriction_form.check_box "#{tag_type}_init_greater_than_average" %>
                </li>
                <li>
                  <%= prompt_restriction_form.label "#{tag_type}_init_factor", ts("Multiply the average by: ") %>
                  <%= prompt_restriction_form.select "#{tag_type}_init_factor", options_for_select(%w{ 0.1 0.25 0.5 0.75 1 5 10 15 20 }, "1") %>
                </li>
              </ul>
            <% end %>

            <% if TagSet::TAG_TYPES_RESTRICTED_TO_FANDOM.include?(tag_type) %>
              <ul>
                <li>
                  <%= prompt_restriction_form.label "#{tag_type}_restrict_to_fandom", ts("Restrict to fandom?") %>
                  <%= prompt_restriction_form.check_box "#{tag_type}_restrict_to_fandom", :checked => restriction.send("#{tag_type}_restrict_to_fandom") %>
                </li>
              </ul>
            <% end %>
          </fieldset>
        <% end %>
      <% end %>
      
    </fieldset>
  <% end %>

<% end %>
